Ext.onReady(function() {

    Ext.QuickTips.init();
    //元数据的store
    var store2 = new Ext.data.Store({
        storeId: 'store2',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=cost'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取部门列表
    var storedepartment = new Ext.data.Store({
        storeId: 'storedepartment',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=department'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });
    var cardOrArea_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_AreaOrCard_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    store2.load();
    storedepartment.load();

    var form = new Ext.FormPanel({
        labelAlign: 'form',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: '100%',
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .4,
                        layout: 'form',
                        items: [{
                                xtype: 'datefield',
                                altFormats: 'Ymd',
                                format: 'Ymd',
                                dateFormat: 'Ymd',
                                fieldLabel: '起始日期',
                                name: 'start_time',
                                id: 'start_time',
                                emptyText: '起始日期',
                                anchor: '95%'
                            }, new Ext.ux.form.LovCombo({
                                fieldLabel: '费用项目',
                                name: 'type',
                                id: 'type',
                                displayField: 'name',
                                valueField: 'value',
                                emptyText: '费用项目',
                                anchor: '95%',
                                typeAhead: true,
                                triggerAction: 'all',
                                store: store2
                            })]
                    }, {
                        columnWidth: .4,
                        layout: 'form',
                        items: [{
                                xtype: 'datefield',
                                altFormats: 'Ymd',
                                format: 'Ymd',
                                dateFormat: 'Ymd',
                                fieldLabel: '截止日期',
                                emptyText: '截止日期',
                                name: 'end_time',
                                id: 'end_time',
                                anchor: '95%'
                            }, new Ext.form.ComboBox({
                                fieldLabel: '部门',
                                emptyText: '部门',
                                displayField: 'name',
                                valueField: 'name',
                                store: storedepartment,
                                triggerAction: 'all',
                                name: 'department',
                                id: 'department',
                                anchor: '95%'
                            }), {
                                xtype: 'hidden',
                                fieldLabel: '汇总类型',
                                emptyText: '汇总类型',
                                name: 'born_end',
                                id: 'born_end',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .1,
                        layout: 'form',
                        items: [{
                                xtype: "button",
                                id: 'query_tbar',
                                text: "查  询",
                                width: 60,
                                handler: function() {
                                    store.setBaseParam("start_time", Ext.util.Format.date(Ext.ComponentMgr.get("start_time").getValue(), 'Ymd'));
                                    store.setBaseParam("end_time", Ext.util.Format.date(Ext.ComponentMgr.get("end_time").getValue(), 'Ymd'));
                                    store.setBaseParam("type", Ext.ComponentMgr.get("type").getValue());
                                    store.setBaseParam("department", Ext.ComponentMgr.get("department").getValue());
                                    store.load();
                                }
                            }]
                    }]
            }]
    });

    jReader1 = new Ext.data.JsonReader({
        root: 'data',
        totalProperty: 'totalCount',
        fields: [{
                name: 'car_code'
            }, {
                name: 'date'
            }, {
                name: 'code'
            }, {
                name: 'name'
            }, {
                name: 'remark'
            }, {
                name: 'cost'
            }]
    });

    var store = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'CostJson.action'
        }),
        reader: jReader1,
        baseParams: {
        }
    });

    var sm = new Ext.grid.CheckboxSelectionModel({
    });

    var cm = new Ext.grid.ColumnModel([sm, {
            header: '车牌号码',
            sortable: true,
            dataIndex: 'car_code'
        }, {
            header: '费用时间',
            sortable: true,
            dataIndex: 'date'
        }, {
            header: '费用项目编号',
            sortable: true,
            dataIndex: 'code'
        }, {
            header: '费用项目名称',
            sortable: true,
            dataIndex: 'name'
        }, {
            header: '费用摘要',
            sortable: true,
            dataIndex: 'remark'
        }, {
            header: '费用金额',
            sortable: true,
            dataIndex: 'cost'
        }]);

    var grid = new Ext.grid.GridPanel({
        id: 'ggg',
        store: store,
        anchor: '100% -80',
        viewConfig: {
            forceFit: true
        },
        sm: sm,
        cm: cm,
        frame: true,
        columnLines: true,
        autoWidth: true,
        loadMask: true, // 数据加载时的遮罩功能
        stateful: true,
        stateId: 'grid',
        pageSize: 20,
        bbar: new Ext.PagingToolbar({
            store: store,
            emptyMsg: '无数据',
            displayInfo: true
        })
    });
    store.load();

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
                region: 'center',
                layout: 'border',
                items: [{
                        region: 'center',
                        // layout : 'accordion',
                        layout: "anchor",
                        frame: true,
                        deferredRender: false,
                        activeTab: 0,
                        tbar: new Ext.Toolbar({
                            items: ['-', {
                                    text: '导出Excel',
                                    handler: function() {
                                        var obj = {
                                            start_time: Ext.util.Format.date(Ext.ComponentMgr.get("start_time").getValue(), 'Ymd'),
                                            end_time: Ext.util.Format.date(Ext.ComponentMgr.get("end_time").getValue(), 'Ymd'),
                                            type: Ext.ComponentMgr.get("type").getValue(),
                                            department: Ext.ComponentMgr.get("department").getValue()
                                        };
                                        FormUtils("ExportXls_Cost.action", obj);
                                    }
                                }, '-']
                        }),
                        items: [form, grid]
                    }]
            }]
    });
});